package com.xinke.module.system.service.logger;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.xinke.module.system.model.vo.logger.*;
import com.xinke.module.system.model.entity.logger.OperateLogDO;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.util.object.BeanUtils;

import com.xinke.module.system.mapper.logger.OperateLogMapper;

import static com.xinke.component.common.exception.util.ServiceExceptionUtil.exception;
import static com.xinke.module.system.enums.ErrorCodeConstants.*;

/**
 * 操作日志记录 V2 版本 Service 实现类
 *
 * @author yzx
 */
@Service
@Validated
public class OperateLogServiceImpl implements OperateLogService {

    @Resource
    private OperateLogMapper operateLogMapper;

    @Override
    public Long createOperateLog(OperateLogSaveReqVO createReqVO) {
        // 插入
        OperateLogDO operateLog = BeanUtils.toBean(createReqVO, OperateLogDO.class);
        operateLogMapper.insert(operateLog);
        // 返回
        return operateLog.getLogId();
    }

    @Override
    public void updateOperateLog(OperateLogSaveReqVO updateReqVO) {
        // 校验存在
        validateOperateLogExists(updateReqVO.getLogId());
        // 更新
        OperateLogDO updateObj = BeanUtils.toBean(updateReqVO, OperateLogDO.class);
        operateLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteOperateLog(Long logId) {
        // 校验存在
        validateOperateLogExists(logId);
        // 删除
        operateLogMapper.deleteById(logId);
    }

    private void validateOperateLogExists(Long logId) {
        if (operateLogMapper.selectById(logId) == null) {
            throw exception(null);
        }
    }

    @Override
    public OperateLogDO getOperateLog(Long logId) {
        return operateLogMapper.selectById(logId);
    }

    @Override
    public PageResult<OperateLogDO> getOperateLogPage(OperateLogPageReqVO pageReqVO) {
        return operateLogMapper.selectPage(pageReqVO);
    }

}